package com.example.administrator.wechatdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    MyFragment myFragment;
    FindFragment findFragment;
    WeChatFragment weChatFragment;
    ContactsFragment contactsFragment;

    List<Fragment> fragmentList = new ArrayList<>();
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findFragment = new FindFragment();
        myFragment = new MyFragment();
        weChatFragment = new WeChatFragment();
        contactsFragment = new ContactsFragment();

        fragmentList.add(weChatFragment);
        fragmentList.add(contactsFragment);
        fragmentList.add(findFragment);
        fragmentList.add(myFragment);

        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };

        viewPager = (ViewPager) findViewById(R.id.id_container);

        viewPager.setAdapter(adapter);

    }
    public void buttonClick(View view) {
        int id= view.getId();
        switch (id){
            case R.id.id_wechat:{
                //FragmentManager fm = getSupportFragmentManager();
                //FragmentTransaction transaction = fm.beginTransaction();
                //transaction.replace(R.id.id_container,weChantFragment);
                //transaction.commit();
                viewPager.setCurrentItem(0);
                break;
            }
            case R.id.id_contacts:{
                //FragmentManager fm = getSupportFragmentManager();
                //FragmentTransaction transaction = fm.beginTransaction();
                //transaction.replace(R.id.id_container,contactsFragment);
                //transaction.commit();
                viewPager.setCurrentItem(1);
                break;
            }
            case R.id.id_find:{
                //FragmentManager fm = getSupportFragmentManager();
                //FragmentTransaction transaction = fm.beginTransaction();
                //transaction.replace(R.id.id_container,findFragment);
                //transaction.commit();
                viewPager.setCurrentItem(2);
                break;
            }
            case R.id.id_my:{
                // FragmentManager fm = getSupportFragmentManager();
                //FragmentTransaction transaction = fm.beginTransaction();
                //transaction.replace(R.id.id_container,myFragment);
                //transaction.commit();
                viewPager.setCurrentItem(3);
                break;
            }
        }
    }
}
